//
//  APNSettingViewController.m
//  EZ Tracker
//
//  Created by FENGYL on 2016/11/1.
//  Copyright © 2016年 FENGYL. All rights reserved.
//

#import "APNSettingViewController.h"
#import "MsgCommand.h"

#import <SVProgressHUD.h>

#define NUM @"0123456789"

@interface APNSettingViewController ()<UITextFieldDelegate>

@property (strong, nonatomic) MsgCommand *msgCommand;

@property (weak, nonatomic) IBOutlet UIButton *submitButton;
@property (weak, nonatomic) IBOutlet UITextField *countryCodeTextfield;
@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTextfield;
@property (weak, nonatomic) IBOutlet UITextField *apnTextfield;
@property (weak, nonatomic) IBOutlet UITextField *userNameTextfield;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextfield;

@end

@implementation APNSettingViewController

-(MsgCommand *)msgCommand{
    
    if (!_msgCommand) {
        _msgCommand = [[MsgCommand alloc]init];
    }
    return _msgCommand;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpUI];
    
}

#pragma mark 设置UI
-(void)setUpUI{
    
    //圆角按钮
    self.submitButton.layer.masksToBounds = YES;
    
    self.submitButton.layer.cornerRadius = 10;
    
    
    //设置代理
    self.countryCodeTextfield.delegate = self;
    
    self.phoneNumberTextfield.delegate = self;
    
    
}

#pragma mark 收起键盘
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.countryCodeTextfield resignFirstResponder];
    [self.phoneNumberTextfield resignFirstResponder];
    [self.apnTextfield resignFirstResponder];
    [self.userNameTextfield resignFirstResponder];
    [self.passwordTextfield resignFirstResponder];
    
    
    
}

#pragma mark 限制类型
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if (textField == self.countryCodeTextfield || textField == self.phoneNumberTextfield) {
        
        NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:NUM] invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        return [string isEqualToString:filtered];
        
    }
    
    
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark SubmitButton
- (IBAction)touchSubmitButton:(id)sender {
    
    if (!(_phoneNumberTextfield.text.length > 0)) {
        
        [SVProgressHUD showErrorWithStatus:@"Phone number can bot be empty"];
        
        [SVProgressHUD dismissWithDelay:1];
        
        return;
        
    }
    
    if (!(_apnTextfield.text.length > 0)) {
        
        [SVProgressHUD showErrorWithStatus:@"Apn can bot be empty"];
        
        [SVProgressHUD dismissWithDelay:1];
        
        return;
        
        
    }
    
    //号码
    NSString *finalPhoneNumber;
    
    NSString *countryCodeString = _countryCodeTextfield.text;
    NSString *phoneNumberString = _phoneNumberTextfield.text;
    
    if (countryCodeString.length > 0) {
        
        finalPhoneNumber = [NSString stringWithFormat:@"+%@%@",countryCodeString,phoneNumberString];
        
    }else{
        
        finalPhoneNumber = [NSString stringWithFormat:@"%@%@",countryCodeString,phoneNumberString];
        
    }
    
    
    //body
    NSMutableString *bodyString = [NSMutableString string];
    
    [bodyString appendString:@"*apn*"];
    
    [bodyString appendString:_apnTextfield.text];
    
    [bodyString appendString:@"*"];
    
    if (_userNameTextfield.text.length > 0) {
        
        [bodyString appendString:_userNameTextfield.text];
        
        [bodyString appendString:@"*"];
        
    }
    
    if (_passwordTextfield.text.length > 0) {
        
        [bodyString appendString:_passwordTextfield.text];
        
        [bodyString appendString:@"*"];
        
    }
    
    [self.msgCommand showMessageViewForPhoneNumberArray:@[finalPhoneNumber] title:@"New Message" body:bodyString delegate:self];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
